package com.tt.fashionWebSite.service.imp;

import com.tt.fashionWebSite.dao.*;
import com.tt.fashionWebSite.domain.DatHang;
import com.tt.fashionWebSite.domain.HoaDon;
import com.tt.fashionWebSite.domain.ThongTinThanhVien;
import com.tt.fashionWebSite.dto.*;
import com.tt.fashionWebSite.service.Ad_BillService;
import com.tt.fashionWebSite.service.Ad_OrderService;
import com.tt.fashionWebSite.service.GenericService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service("ad_orderService")
public class Ad_OrderServiceImp extends GenericService implements Ad_OrderService {
    @Autowired
    @Qualifier("datHang")
    private DatHangDao datHangDao;

    @Autowired
    @Qualifier("chiTietDatHang")
    private ChiTietDatHangDao chiTietDatHangDao;

    @Autowired
    @Qualifier("thongTinThanhVien")
    private ThongTinThanhVienDao thongTinThanhVienDao;

    @Autowired
    @Qualifier("chiTietSanPham")
    private ChiTietSanPhamDao chiTietSanPhamDao;

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public int adminOrder(DatHangDto orderDto) {
        DatHang datHang = datHangDao.findById(orderDto.getMaDH());
        if(datHang == null){
            datHang = new DatHang();
            datHang.setMaDH(orderDto.getMaDH());
            ThongTinThanhVien thongTinThanhVien = thongTinThanhVienDao.findById(orderDto.getThongTinThanhVien().getMaTV());
            datHang.setThongTinThanhVien(thongTinThanhVien);
            datHang.setHoTenNN(orderDto.getHoTenNN());
            datHang.setDiaChiNN(orderDto.getDiaChiNN());
            datHang.setSoDTNN(orderDto.getSoDTNN());
            datHang.setNgayDat(orderDto.getNgayDat());
            datHang.setNgayGiao(orderDto.getNgayGiao());
            datHang.setHoanTat(orderDto.getHoanTat());
            datHang.setGhiChu(orderDto.getGhiChu());
            datHang.setTongTien(orderDto.getTongTien());
            datHangDao.insert(datHang);
            return JsonResponseCode.SUCCESS;
        }
        else {
            return JsonResponseCode.FAILED;
        }
    }

    public List<DatHangDto> getOrderCommon () {
        return mapper.mapCollection(datHangDao.getOrderCommon(), DatHangDto.class);
    }

    public List<ChiTietDatHangDto> getOrderDetailCommon () {
        return mapper.mapCollection(chiTietDatHangDao.getOrderDetailCommon(), ChiTietDatHangDto.class);
    }

    public List<ThongTinThanhVienDto> getMemberInfor () {
        return mapper.mapCollection(thongTinThanhVienDao.getMemberInfor(), ThongTinThanhVienDto.class);
    }

    public List<ChiTietSanPhamDto> getAllProductDetailCode () {
        return mapper.mapCollection(chiTietSanPhamDao.getAllProductDetail(), ChiTietSanPhamDto.class);
    }
}

